import pymssql
import time

# 创建数据库连接
def createConnection():
	try:
		conn = pymssql.connect(server='127.0.0.1', user='ypb', password='123456', database='backup',charset="GBK")
	except BaseException:
		print("获取数据库连接失败！")
	if(conn):
		print("获取数据库连接成功！")
	return conn

#关闭数据库连接。
def closeConnection(conn):
	conn.close()

# 查询数据
def query(conn):
	#print("开始查询数据")
	cursor = conn.cursor()
	cursor.execute("select * from car;")
	resList = cursor.fetchall()
	if(resList):
		delete(conn)
	return resList

def delete(conn):
	#print("开始删除数据")
	cursor = conn.cursor()
	cursor.execute("delete from car;")
	conn.commit()
	#print("删除数据成功")
	#
def dealRes(p):
	if(p==[]):
		return
	list2=[str(i).replace(" ","") for i in p]
	list3=','.join(list2)
	print(list3)

def main():
	conn = createConnection()
	while True:
		reslist = query(conn)
		dealRes(reslist)
		time.sleep(5)

if __name__ == '__main__':
	main()